package com.seed.web.struts.sysmanager;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.seed.commons.dbquery.PageViewForSQL;
import com.seed.service.sysmanager.IRole;
import com.seed.web.core.BaseActionSupport;
import com.seed.web.form.RoleForm;

@Controller
@Scope("prototype")
public class RoleAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;

	private RoleForm role;
	
	private String[] menuID ;

	public String[] getMenuID() {
		return menuID;
	}

	public void setMenuID(String[] menuID) {
		this.menuID = menuID;
	}

	public RoleForm getRole() {
		return role;
	}

	public void setRole(RoleForm role) {
		this.role = role;
	}

	@Resource(name = "pageViewForSQL")
	private PageViewForSQL pv;

	public PageViewForSQL getPv() {
		return pv;
	}

	public void setPv(PageViewForSQL pv) {
		this.pv = pv;
	}

	@Resource
	private IRole roleService ;

	public String updaterole() throws Exception {
		
		return "" ;
	}
	
	public String setRoles() throws Exception {
		if(null != role && null != menuID && menuID.length > 0) {
			if(this.roleService.setRolesResource(role.getRoleId(), menuID)) {
				saveMessage("角色菜单资源添加成功！");
			} else {
				saveMessage("角色菜单资源添加失败！");
			}
		} else {
			saveMessage("发生错误，请选择菜单资源");
		}
		
		System.out.println(menuID.length);
		
		return "msg" ;
	}
	
	public String saverole() throws Exception {
		boolean flag = this.roleService.saveRole(role);
		if (flag) {
			saveMessage("角色添加成功！");
		} else {
			saveMessage("角色添加 失败！");
		}
		return "msg";
	}

	public String findrole() throws Exception {
		pv.setPageModel(this.roleService.getScrollData(pv.getFirstResult(),
				pv.getLineSize()));
		
		this.getBaseform().getRequest().setAttribute("scrolldata", pv);
		if("userJOINrole".equals(this.getBaseform().getType())) {
			this.getBaseform().getRequest().setAttribute("userid", this.getBaseform().getParam()) ;
			return "userJOINrole" ;
		} else {
			return "findrole";
		}
	}
}
